package com.ptu.spzx.controller;


import com.github.pagehelper.PageInfo;
import com.ptu.spzx.common.exception.ptuException;
import com.ptu.spzx.domain.Delete.DeleteIds;
import com.ptu.spzx.domain.Exam.Exampaper;
import com.ptu.spzx.domain.Exam.dto.AutoAddExamPaper;
import com.ptu.spzx.domain.Exam.dto.ExamPaperDTO;
import com.ptu.spzx.domain.Exam.vo.ExamPaperVo;
import com.ptu.spzx.domain.Stu.Vo.StuAnswerVo;
import com.ptu.spzx.model.vo.common.Result;
import com.ptu.spzx.model.vo.common.ResultCodeEnum;
import com.ptu.spzx.service.IExampaperService;
import org.simpleframework.xml.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author YT
 * @since 2024-01-28
 */
@RestController
@RequestMapping("/sc/exam/epaper")
public class ExampaperController {
    @Autowired
    private IExampaperService exampaperService;

    //获取单张试卷详细信息
    @GetMapping("/selectExamPaper/{epId}")
    @PreAuthorize("@permissionService.hasPermission('exam:epaper:selectExamPaper')")
    public Result selectExamPaper(@PathVariable("epId") Long epId){
        Exampaper exampaper = exampaperService.selectExamPaper(epId);
        return Result.build(exampaper, ResultCodeEnum.SUCCESS);
    }

    //增加试卷
    @PostMapping("/addExamPaper")
    @PreAuthorize("@permissionService.hasPermission('exam:epaper:addExamPaper')")
    public Result addExamPaper(@RequestBody ExamPaperDTO exampaperDTO){
        exampaperService.addExampaper(exampaperDTO);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    //删除试卷
    @DeleteMapping("/deleteExamPaper")
    @PreAuthorize("@permissionService.hasPermission('exam:epaper:deleteExamPaper')")
    public Result deleteExamPaper(@RequestBody DeleteIds deleteIds){
        exampaperService.deleteExamPaper(deleteIds);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    //更新试卷
    @PutMapping("/updateExamPaper")
    @PreAuthorize("@permissionService.hasPermission('exam:epaper:updateExamPaper')")
    public Result updateExamPaper(@RequestBody ExamPaperDTO examPaperDTO){
        if(examPaperDTO.getEpId()==null){
            throw new ptuException(ResultCodeEnum.PARAM_NULL);
        }
        exampaperService.updateExamPaper(examPaperDTO);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    //根据课程或教师获取试卷
    @GetMapping("/getExamPaperByCreator/{courseId}/{pageNum}/{pageSize}")
    @PreAuthorize("@permissionService.hasPermission('exam:epaper:getExamPaperByCreator')")
    public Result getExamPaperByCreator(@PathVariable("courseId") Integer courseId,
                                        @RequestParam("creator") String creator,
                                        @PathVariable("pageNum") Integer pageNum,
                                        @PathVariable("pageSize") Integer pageSize){
        PageInfo<Exampaper> examPaperByCreator = exampaperService.getExamPaperByCreator(courseId, creator,pageNum,pageSize);
        return Result.build(examPaperByCreator, ResultCodeEnum.SUCCESS);
    }


    //学生获取试卷不带答案
    @GetMapping("/getExampaperWithAnswer/{examId}/{stuId}")
    @PreAuthorize("@permissionService.hasPermission('exam:epaper:getExampaperWithAnswer')")
    public Result getExampaperWithAnswer(@PathVariable("examId") Long examId, @PathVariable("stuId") String stuId){
        ExamPaperVo exampaperWithAnswer = exampaperService.getExampaperWithAnswer(examId, stuId);
        return Result.build(exampaperWithAnswer,ResultCodeEnum.SUCCESS);
    }

    //教师随机获取学生试卷
    @GetMapping("/getStuAnswer/{examId}")
    @PreAuthorize("@permissionService.hasPermission('exam:epaper:getStuAnswer')")
    public Result getStuAnswer(@PathVariable("examId") Long examId){
        StuAnswerVo stuAnswer = exampaperService.getStuAnswer(examId);
        return Result.build(stuAnswer,ResultCodeEnum.SUCCESS);
    }

    //通过考试ID查询试卷
    @GetMapping("/getExamPaperByExamId/{examIds}/{pageNum}/{pageSize}")
    @PreAuthorize("@permissionService.hasPermission('exam:epaper:getExamPaperByExamId')")
    public Result getExamPaperByExamId(@PathVariable("examIds") List<Long> examIds,
                                       @PathVariable("pageNum") Integer pageNum,
                                       @PathVariable("pageSize") Integer pageSize){
        PageInfo<Exampaper> exampapers = exampaperService.getExamPaperByExamId(examIds,pageNum,pageSize);
        return Result.build(exampapers,ResultCodeEnum.SUCCESS);
    }

    //根据课程获取试卷列表
    @GetMapping("/getExamPaperByCourseId/{courseIds}/{pageNum}/{pageSize}")
    @PreAuthorize("@permissionService.hasPermission('exam:epaper:getExamPaperByCourseId')")
    public Result getExamPaperByCourseId(@PathVariable("courseIds") List<Long> courseIds,
                                         @PathVariable("pageNum") Integer pageNum,
                                         @PathVariable("pageSize") Integer pageSize){
        PageInfo<Exampaper> exampapers= exampaperService.getExamPaperByCourseId(courseIds,pageNum,pageSize);
        return Result.build(exampapers,ResultCodeEnum.SUCCESS);
    }

    //自动组卷
    @PostMapping("/autoAddExamPaper")
    @PreAuthorize("@permissionService.hasPermission('exam:epaper:autoAddExamPaper')")
    public Result autoAddExamPaper(@RequestBody AutoAddExamPaper autoAddExamPaper){
        exampaperService.autoAddExamPaper(autoAddExamPaper);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

}

